#include "scrollarea.h"
#include "ui_scrollarea.h"

scrollarea::scrollarea(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::scrollarea)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    label= new QLabel();
    label->clear();
}

void scrollarea::set_image(QPixmap *image)
{
    /*QSize imagesize = reader.size();
    reader.setScaledSize(imagesize);
    QImage image =reader.read();
    QPixmap img= QPixmap::fromImage(image);*/
    label->setPixmap(*image);
    ui->scrollArea->setWidget(label);
    ui->scrollArea->setAlignment(Qt::AlignCenter);
    ui->scrollArea->setWidgetResizable(true);
}

void scrollarea::mouseReleaseEvent(QMouseEvent *)
{
    delete label;
    ui->scrollArea->close();
    this->close();
    delete ui;
}

void scrollarea::mouseMoveEvent(QMouseEvent *event)
{
    int x = event->x();
    int y = event->y();
    ui->scrollArea->horizontalScrollBar()->setValue(x);
    ui->scrollArea->verticalScrollBar()->setValue(y);
}

scrollarea::~scrollarea()
{
    delete ui;
}

void scrollarea::show_image(QString file_name)
{
    QFile my_infile(file_name);
    if(my_infile.exists())
    {
        QPixmap aa(file_name);
        label->setPixmap(aa);
        label->resize(QSize(aa.width(),aa.height()));
        ui->scrollArea->setWidget(label);
        ui->scrollArea->setAlignment(Qt::AlignCenter);
        ui->scrollArea->setWidgetResizable(true);
    }
}
